package com.foreknow.controller;

import com.foreknow.bean.Guestbook;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;
import com.foreknow.util.Pagination;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ManagerServlet extends HttpServlet {
    private int pageSize = 10;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取到请求的参数q=list
        String method = req.getParameter("q");
        if("list".equals(method)){
            list(req,resp);
        }else if("delete".equals(method)){
            delete(req,resp);
        }else if("update".equals(method)){
            update(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        GuestbookService service = new GuestbookServiceImpl();
        service.deleteGuestbookById(Integer.parseInt(id));
        req.getRequestDispatcher("/user/manager?q=list").forward(req,resp);
    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GuestbookService service = new GuestbookServiceImpl();
        //1.要获取到每页显示的记录数(PageSize)

        //2.从页面获取页码(PageNumber)
        int pageNumber = 0;
        //如何获取到页码
        //先要获取到请求参数的名称d-5429-p
//		String paramName = new ParamEncoder("gb").encodeParameterName(TableTagParameters.PARAMETER_PAGE);
        //根据这个名称paramName才能获取到请求的参数
        try {
            pageNumber = Integer.parseInt(req.getParameter("pageNo"));
        } catch (Exception e) {
            pageNumber = 1;
        }
        Pagination pagination = service.getByPage(pageSize, pageNumber);
        MappingFactory mappingFactory = MappingFactory.getInstance();
        EntityMapping mapping = mappingFactory.mapping(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = pagination.getList(mapping);
        //3.将总记录数，总页数，每页显示的数据（List）保存起来
        req.setAttribute("total", pagination.getMaxElements());
        req.setAttribute("list", list);
        //转发资源
        req.getRequestDispatcher("/list.jsp").forward(req, resp);
    }

//    @Override
//    public void init(ServletConfig config) throws ServletException {
//      this.pageSize =Integer.parseInt(config.getServletContext().getInitParameter("pageSize")) ;
//    }
}
